/**
* fixTableHeight
* 动态根据页面高度设置el-table的高度
*/
export default {
    mounted(el, binding) {
      el.windowChange = () => {
        setHeight(el, binding)
      };
      el.windowChange();
      window.addEventListener('resize', el.windowChange);
      let _el = el;
      let _binding = binding;
      // 监听home-content的大小变化
      const ro = new ResizeObserver((entries, observer) => {
        for (const entry of entries) {
          const { contentRect } = entry;
          // console.log('Element resized:', contentRect.height);
          setHeight(_el, _binding)
        }
      });
      // 获取监听的DOM元素
      const element = document.querySelector('.home-content')
      // 设置监听
      ro.observe(element)
    },
    updated(el, binding) {
      el.windowChange();
    },
    beforeUnmount(el) {
      window.removeEventListener('resize', el.windowChange);
      clearTimeout(el.timer);
      delete el.timer;
      delete el.windowChange;
    },
  };
  
  function setHeight(el, binding) {
    if (document.querySelector('.my-pagination')) {
      let paginationH = document.querySelector('.my-pagination').parentNode.clientHeight;
      let paginationMargin = Number(document.querySelector('.my-pagination')?.parentNode?.style['margin']?.split(' ')[0].replace(new RegExp('px'), '')) * 2;
      let bottomHeight = binding?.value || paginationH + paginationMargin + 16;
      el.timer && clearTimeout(el.timer);
      el.timer = setTimeout(() => {
        el.style.transition = 'height 0.8s';
        el.style.height = `${window.innerHeight - el.offsetTop - bottomHeight
          }px`;
        el.style.overflowY = 'auto'
        setTimeout(() => {
          el.style.transition = '';
        }, 1000);
      }, 100);
    }
  }